﻿using LeafAssist;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlatformDemo.Demo.Customer
{
    public partial class GetCustomers : UserControl
    {
        public GetCustomers()
        {
            InitializeComponent();

            textBoxUrl.Text = GlobalVariable.BaseUrl;
            textBoxParams.Text = GlobalVariable.BaseRoute["GetCustomers"];

            buttonGo.Click += (object sender, EventArgs eventArgs) =>
            {
                textBoxResult.Text = string.Empty;
                labelHttpStatus.ForeColor = SystemColors.ControlText;

                var httpRequest = new HttpRequest();

                var urlParams = new RequestParams
                {
                    //["since_id"] = textBoxlimitId.Text,
                    //["created_at_min"] = textBoxlimitTimeMin.Text,
                    //["created_at_max"] = textBoxlimitTimeMax.Text,
                    //["limit"] = textBoxLimit.Text,
                    //["page"] = textBoxPage.Text,
                    ["fields"] = textBoxFields.Text,
                };

                // fields
                // billing_address,shipping_address,addresses,customer_guid,usernameemail,
                // first_name,last_name,language_id,date_of_birth,gender,admin_comment,is_tax_exempt,
                // has_shopping_cart_items,active,deleted,is_system_account,system_name,last_ip_address,
                // created_on_utc,last_login_date_utc,last_activity_date_utc,registered_in_store_id,
                // subscribed_to_newsletter,role_ids

                string url = string.Format("{0}{1}", textBoxUrl.Text, textBoxParams.Text);

                httpRequest.UserAgent = Http.FirefoxUserAgent();

                if (Reflection.Token == null)
                {
                    string message = "当前令牌为空！\n请先获取令牌！！！";
                    string caption = "错误";
                    MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                    DialogResult result;

                    result = MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Error);
                    return;
                }

                httpRequest.Authorization = $"{Reflection.Token.token_type} {Reflection.Token.access_token}";

                new Thread(Action =>
                {
                    Application.DoEvents();
                    textBoxResult.Invoke(new Action(() =>
                    {
                        HttpResponse httpResponse = httpRequest.Get(url, urlParams);

                        switch (httpResponse.StatusCode)
                        {
                            case HttpStatusCode.OK:
                                labelHttpStatus.ForeColor = Color.Green;
                                textBoxResult.Text = httpResponse.ToString();
                                break;
                            default:
                                labelHttpStatus.ForeColor = Color.Red;
                                break;
                        }

                        labelHttpStatus.Text = $"响应码: {(int)httpResponse.StatusCode}";
                    }));
                }).Start();
            };
        }
    }

    public class GetAllCustomersInterface : Interface
    {
        public string Name => "用户列表";

        public string Category => "用户";

        public Control CreatePanel()
        {
            return new GetCustomers();
        }
    }
}
